package com.example.baseapp.service.impl;

import com.example.baseapp.entity.Category;
import com.example.baseapp.entity.Dish;
import com.example.baseapp.repository.CategoryRepository;
import com.example.baseapp.repository.DishRepository;
import com.example.baseapp.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private CategoryRepository categoryRepository;
    @Autowired
    private DishRepository dishRepository;

    @Override
    public List<Map<String, Object>> getMenuGroupedByCategory() {
        List<Category> categories = categoryRepository.findAll();
        List<Dish> dishes = dishRepository.findByStatus(1); // 只查上架菜品
        List<Map<String, Object>> result = new ArrayList<>();
        for (Category category : categories) {
            Map<String, Object> group = new HashMap<>();
            group.put("type", category.getName());
            List<Dish> dishList = dishes.stream()
                .filter(d -> Objects.equals(d.getCategoryId(), category.getId()))
                .collect(Collectors.toList());
            group.put("dishes", dishList);
            result.add(group);
        }
        return result;
    }
} 